<%@page import="com.tubeonfire.model.AdvertisingModel"%>
<%@page import="com.tubeonfire.entity.Advertising"%>
<%@page import="com.google.appengine.api.users.User"%>
<%@page import="com.google.appengine.api.users.UserServiceFactory"%>
<%@page import="com.google.appengine.api.users.UserService"%>
<%@page import="com.tubeonfire.model.admin.SiteConfigModel"%>
<%@page import="com.tubeonfire.entity.SiteConfig"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="com.google.appengine.api.images.ImagesServiceFactory"%>
<%@page import="com.google.appengine.api.images.ImagesService"%>
<%@page import="com.google.appengine.api.blobstore.BlobKey"%>
<%@page import="com.tubeonfire.search.TubeSearchModel"%>
<%@page import="com.tubeonfire.entity.Tube"%>
<%@page import="java.util.*"%>
<%	
	request.setCharacterEncoding("UTF-8");
	TubeSearchModel model = (TubeSearchModel) request.getAttribute("model");
	UserService userService = UserServiceFactory.getUserService();
	User user = userService.getCurrentUser();
	String tag = (String) request.getAttribute("tag");	
	ImagesService imagesService = ImagesServiceFactory.getImagesService(); 	
	SiteConfig siteConfig = SiteConfigModel.get();	
	String logoUrl = siteConfig.getLogoKey();
	if(!logoUrl.equals("/images/logo.png")){ 
		logoUrl = "http://" + request.getServerName() + "/image?key=" + logoUrl + "&size=250"; 	
	}	
	String pageUrl = (String)request.getAttribute("url");
	String pageTitle = tag + " - Page " + model.getPage();
	String pageName = siteConfig.getSiteName();
	String pageDescription = pageTitle + ". " + siteConfig.getSiteDescription();
	String pageKeyword = tag + ", " + siteConfig.getSiteKeyword();
	String pageImage = logoUrl;
	String facebookAdminId = siteConfig.getFacebookAdminId();	
	String pageLogoUrl = logoUrl;
	String pageLogoSlogan = siteConfig.getLogoSlogan();
	String analyticCode = siteConfig.getAnalyticCode();
	int place = 6;
	int position = 1;
%>
<jsp:include page="/element/header.jsp">		
	<jsp:param name="pageUrl" value="<%= pageUrl %>"/>
	<jsp:param name="pageTitle" value="<%=pageTitle %>"/>	
	<jsp:param name="pageName" value="<%=pageName %>"/>
	<jsp:param name="pageImages" value="<%=pageImage %>"/>
	<jsp:param name="pageKeyword" value="<%=pageKeyword %>"/>
	<jsp:param name="pageDescription" value="<%=pageDescription %>"/>
	<jsp:param name="facebookAdminId" value="<%=facebookAdminId %>"/>
	<jsp:param name="pageLogoUrl" value="<%=pageLogoUrl %>"/>
	<jsp:param name="pageLogoSlogan" value="<%=pageLogoSlogan %>"/>
</jsp:include>
<div id="content">
	<div id="content_inner">
    	<div id="leftsidecontent">
            <section>
                <div class="block">
                    <div class="search-result">  
                    	<h3 class="button green"><a href="#"><%=tag %></a></h3>
                    	<%                    		
                    		Advertising obj = AdvertisingModel.getByPlaceAndPosition(place, position);
                    	%>
                    	<%
	                    	if(obj!=null&&obj.getId().length()>0){
	                    		%>
	                    		<br/>	                    		
		                    	<div class="advWrap adv-468x60 clearafter" style="border: none;">
			                    	<div>
			                    		<%
				          					if(obj.getType()==1){
				          						%>
				          						<%=obj.getContent() %>	
				          						<%
				          					}else{
				          						String imgUrl = obj.getContent();
				        						imgUrl = "/image?key=" + imgUrl + "&size=468";
				          						%>
				          						<a title="<%=obj.getTitle()%>" href="<%=obj.getLink()%>">
				          							<img alt="<%=obj.getTitle() %>" src="<%=imgUrl%>">
				          						</a> 
				          						<%
				          					}
				          				%>    
									</div>
			                    </div>  		
	                    		<%
	                    	}else if(user!=null&&userService.isUserAdmin()){
	                    		%>
	                    		<br/>
		                    	<div class="advWrap adv-468x60 clearafter">
			                    	<div>
			                    		<a href="/admin/advertising/add?place=<%=place %>&position=<%=position %>" title="Add Advertising">Add Advertising Here (468 x 60)</a>
						             </div>
			                    </div>
	                    		<%
	                    	}
	                    %>
                        <div class="video-listing clearafter">
                        <%
	                        if(model!=null&&model.getListResult().size()>0){
	                        	for(Tube tub : model.getListResult()){
	                        		String imgUrl = tub.getImageUrl();
									if(!imgUrl.contains("http://")){
										try{
											BlobKey blobKey = new BlobKey(imgUrl);
											imgUrl = imagesService.getServingUrl(blobKey);
										}catch(Exception e){
											imgUrl = "/images/tube.png";
										}														
									}
                        %>
                       		<div class="video-items" id="<%=tub.getId()%>">
                               <div class="video-items_inner">
                                   <a class="link-image-scale" href="/video/<%=tub.getId() %>/<%=tub.getAlias() %>.html" title="<%=tub.getTitle()%>">
                                       <img class="imageToScale" src="<%=imgUrl%>" alt="<%=tub.getTitle()%>" title="<%=tub.getTitle()%>"/>
                                   </a>               
                                   <h3 class="video-title"><a href="/video/<%=tub.getId() %>/<%=tub.getAlias() %>.html"><%=tub.getSubTitle() %></a></h3>
                                   <div class="preview">
                                       <a href="/video/<%=tub.getId() %>/<%=tub.getAlias() %>.html" class="button green time"><span class="icon-clock"><%=tub.getTotalTime() %></span></a>
                                       <a href="http://www.youtube.com/embed/<%=tub.getId() %>?autoplay=1&loop=1&playlist=<%=tub.getId() %>" class="button green repeat various fancybox.iframe"><span class="icon-monitor">Cinematic</span></a>
                                   </div>
                               </div>
                           </div>  
                        <%
	                        	}
	                        }
                        %>     
                        <%
                    		position = 2;
                    		obj = AdvertisingModel.getByPlaceAndPosition(place, position);
                    	%>
                    	<%
	                    	if(obj!=null&&obj.getId().length()>0){
	                    		%>	                    			                    	
		                    	<div class="advWrap adv-468x60 clearafter" style="border: none;">
			                    	<div>
			                    		<%
				          					if(obj.getType()==1){
				          						%>
				          						<%=obj.getContent() %>	
				          						<%
				          					}else{
				          						String imgUrl = obj.getContent();
				        						imgUrl = "/image?key=" + imgUrl + "&size=468";
				          						%>
				          						<a title="<%=obj.getTitle()%>" href="<%=obj.getLink()%>">
				          							<img alt="<%=obj.getTitle() %>" src="<%=imgUrl%>">
				          						</a> 
				          						<%
				          					}
				          				%>    
									</div>
			                    </div> 
			                    <br/> 		
	                    		<%
	                    	}else if(user!=null&&userService.isUserAdmin()){
	                    		%>	                    		
		                    	<div class="advWrap adv-468x60 clearafter">
			                    	<div>
			                    		<a href="/admin/advertising/add?place=<%=place %>&position=<%=position %>" title="Add Advertising">Add Advertising Here (468 x 60)</a>
						             </div>
			                    </div>			                    
	                    		<%
	                    	}
	                    %>                 
                        </div>
                    </div>
                </div>
            </section>
            <%
            	if(model.getTotalPage()>1){		
            %>
           		<ul class="paginate">           		
				<%
					if(model.getPage()>1){
						%>
						<li><a class="first" href="<%=pageUrl%>?page=1">First</a></li>
						<li><a class="first" href="<%=pageUrl%>?page=<%=model.getPage()-1%>">Previous</a></li>
						<%
					}				
            		int startPage = 1;
           			int endPage = 7;
           			int currentPage = model.getPage();
           			int totalPage = model.getTotalPage();
           			if(currentPage<=3){
           				startPage = 1;
           				if(totalPage >= 7){
           					endPage = 7;	
           				}else{
           					endPage = totalPage;
           				}	                    					
           			}else{	
           				startPage = currentPage - 3;
           				if((totalPage-currentPage)>3){
           					endPage = currentPage + 3;	
           				}else{
           					endPage = totalPage;
           					startPage = totalPage - 6;
           					if(startPage<=0){
           						startPage = 1;
           					}	                    					
           				}		                    			
           			}	 
           			for(int i = startPage;i<=endPage;i++){
           			if(model.getPage()==i){
           			%>  
              			<li><a class="current" href="<%=pageUrl%>?page=<%=model.getPage()%>"><%=i %></a></li>
         			<%
           			}else{
           				%>
           				<li><a href="<%=pageUrl%>?page=<%=i %>"><%=i %></a></li>
           				<%
           			}
           		}	                    			           		
					if(model.getPage()<model.getTotalPage()){
				%>
					<li><a href="<%=pageUrl%>?page=<%=model.getPage()+1%>">Next</a></li>
					<li><a href="<%=pageUrl%>?page=<%=model.getTotalPage()%>">Last</a></li>
				<%
					}
				%>				
				</ul>
			<%
            	}
			%>
        </div>
        <jsp:include page="/element/right-content-tag.jsp">
        	<jsp:param value="<%=(user!=null&&userService.isUserAdmin())?'1':'0' %>" name="isAdmin"/>
        </jsp:include>   
        <div class="clearbreak"></div>        
    </div>
</div>
<script type="text/javascript">		
	$(document).ready(function() {
		$(".various").fancybox({
			maxWidth	: 800,
			maxHeight	: 600,
			fitToView	: false,
			width		: '60%',
			height		: '60%',
			autoSize	: false,
			closeClick	: true,
			closeBtn    : false,
			openEffect	: 'fade',
			closeEffect	: 'fade'
		});	
	});					
</script>
<jsp:include page="/element/footer.jsp">
	<jsp:param name="analyticCode" value="<%= analyticCode %>"/>
</jsp:include>